<?
define('INCLUDE_CHECK',true);
require 'connect.php';
session_name('tzLogin');
session_start();  

switch($action) 
{
    case 'delete' : 
        $doc_id = $_POST['doc_id']; 
        mysql_query("DELETE FROM documents WHERE doc_id = $doc_id") or die(mysql_error()); 
        break; 

    case 'rename' : 
        include_once('utility.class.php'); 
        $doc_id = $_POST['doc_id']; 
        $doc_name = mysql_real_escape_string(htmlspecialchars($_POST['new_doc_name']));
        if(strlen($doc_name)<1)
	{
	    echo 'Please enter a project name.';
	}
        else if (strlen($doc_name) > 255)
        { 
            echo "Document name too long, we limit to 255 characters."; 
        }
        else 
        {   
            $utility = new Utility(); 
            date_default_timezone_set($utility->getLocalTimezone());

            $date = date('Y-m-d G:i:s');
            $query = "UPDATE documents SET doc_name='$doc_name', date_updated='$date' WHERE doc_id=$doc_id"; 
            mysql_query($query);
        }
        break; 
    
    case 'create' : 
        include_once('utility.class.php'); 
        $doc_name = mysql_real_escape_string(htmlspecialchars($_POST['doc_name']));
        $content = addslashes("/* 
* This is example code to help you get started
* writing a new project.
*
* Edit this file and then click run!
*/
 
var x = 100; 
var y = 0; 
while (x > 0)
{ 
    x--; 
    y++; 
}
print(y);"); 
        if(strlen($doc_name)<1)
	{
	    echo 'Please enter a project name.';
	}
        else if(strlen($doc_name) > 255 ) 
        { 
            echo 'Document name too long, we limit to 255 characters.'; 
        }
        else
        { 
            $utility = new Utility(); 
            date_default_timezone_set($utility->getLocalTimezone());

            $date = date('Y-m-d G:i:s');
	    mysql_query("	INSERT INTO documents(user_id,doc_name,date_created,date_updated,content)
				VALUES(

					'".$_SESSION['id']."',
					'".$doc_name."',
					'$date',
					'$date', 
                                        '".$content."'
				      )");


	    if (mysql_affected_rows($link)!=1)
	    {
                echo 'Document creation failed please try again.';
	    }
        }
        break; 
    default : 
        break;  
}
?>
